import { defineStore } from 'pinia'
import { setTokens } from "@/utils/auth"
import { loginApi } from '@/api/user/user'
import { LoginParm } from '@/api/user/UserModel'
import { Result } from "@/utils/request"

export type UserState = {
  token: string | number,
  UserInfoList:{}
}
export const userStore = defineStore('userStore', {
  state: (): UserState => {
    return {
      token: '',
      UserInfoList:{} as any
    }
  },
  actions: {
    // 
    login(loginParm: LoginParm) {
      return new Promise<Result>((resolve, reject) => {
        loginApi(loginParm).then((res) => {
          if (res.code === 200) {
            this.token = res.data.token
            setTokens(res.data.token)
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  getters: {},
  persist: {
    storage: sessionStorage,
    paths: ['token','UserInfoList'],
  }
})